using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace CodePlex.DnD4eCampaignManager.DnD4e
{
    public static class ExtensionMethods
    {
        public static string ToModifierString(this int modifier)
        {
            return modifier >= 0 ? string.Format("+{0}", modifier) : modifier.ToString();
        }

        public static string ToXmlString(this object o)
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF7)
                                              {
                                                  Formatting = Formatting.Indented,
                                                  Indentation = 1,
                                                  IndentChar = '\t'
                                              };
            xmlSerializer.Serialize(xmlTextWriter, o);
            memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
            UTF7Encoding encoding = new UTF7Encoding();
            return encoding.GetString(memoryStream.ToArray()).Replace("><", ">\r\n<");
        }

        public static int TryParseToInt(this string stringToParse)
        {
            int returnValue;
            int.TryParse(stringToParse, out returnValue);
            return returnValue;
        }
    }
}